package share.storage.io.exchange;

import java.util.List;
import org.dom4j.Document;
import org.dom4j.Element;

/**
 * 对交换XML文档进行分析
 * @author WZZ
 *
 */
public class ExchangeXmlResolver {
	Document xml=null;
	Element current=null;
	
	public ExchangeXmlResolver(Document xml){
		this.xml=xml;
	}
	
	/**
	 * 根据path定位到XML元素
	 * @param uri 相对于挂载点的路径
	 * @return 如果存在此元素,返回true,否则返回false
	 */
	public boolean locate(String _path){
		StringBuffer path=new StringBuffer(_path);
		StringBuffer xpath=new StringBuffer("/EnterPoint");
		while(path.length()>0&&path.charAt(0)=='/')
			path.deleteCharAt(0);
		String[] tokens=path.toString().split("/");
		for(String token:tokens){
			if(token.length()<=0)
				continue;
			xpath.append("/File[@name='"+token+"']");
		}
		Element e=(Element) xml.selectSingleNode(xpath.toString());
		if(e!=null){
			current=e;
			return true;
		}
		return false;
	}
	
	@SuppressWarnings("unchecked")
	public List<Element> getChildren(){
		return current.elements("File");
	}
	
	public Element getCurrent(){
		return current;
	}
	
}
